package m.jclecai.com.activity;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2018/2/26.
 */

public class AlterNicknameActivity extends BaseActivity {

    @BindView(R.id.et_nickname)
    EditText mEtNickname;
    @BindView(R.id.tv_sure)
    TextView mTvSure;

    @Override
    protected int getContentView() {
        return R.layout.activity_alter_name;
    }

    @Override
    protected void initView() {
        super.initView();
        leftOptionText("");
        setTitle("修改昵称");
    }

    UserBean userBean;
    @Override
    protected void initData() {
        super.initData();
        userBean = PreferenceUtil.readUser(this);
        mEtNickname.setText(userBean.getNickname());
        mEtNickname.setSelection(userBean.getNickname().length());
    }

    @OnClick(R.id.tv_sure)
    public void onClickSure(){
        final String nickname = mEtNickname.getText().toString().trim();
        if(TextUtils.isEmpty(nickname)){
            DisPlay("昵称不能为空!!!");
            return;
        }

        userBean = PreferenceUtil.readUser(this);

        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user", PreferenceUtil.getUserName(this));
        paramsMap.put("password",PreferenceUtil.getPassword(this));
        paramsMap.put("name",nickname);

        showDialog();
        HttpManager.postAsyn(Constant_Url.SET_NICKNAME, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
            }

            @Override
            public void onResponse(String response) {

                dissDialog();

                if (!TextUtils.isEmpty(response)) {
                    ProofBean proofBean = FastOk.get(response, ProofBean.class);
                    if (proofBean != null){
                        if (proofBean.getStatus().equals("0")) {
                            //这里表示修改成功;
                            DisPlay("修改成功");
                            userBean.setNickname(nickname);
                            PreferenceUtil.writeUser(AlterNicknameActivity.this,userBean);
                            finish();
                        }else{
                            //这里表示修改失败；
                            DisPlay(proofBean.getMessage());
                        }
                    }
                    else
                        DisPlay(getString(R.string.error_network));

                }else{
                    DisPlay("网络异常!!!");
                }
            }
        },paramsMap);

    }

}
